package com.cfets.annualAssessment.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实体类转化成Map集合
 * @author HEQUAN
 *
 */
public class MapUtil {

    public static Map<String,Object> convertObjToMap(Object obj){
        Map<String,Object> reMap = new HashMap<String,Object>();
        if (obj == null){
            return null;
        }

        Field[] fields = obj.getClass().getDeclaredFields();
        try {
            for(int i=0;i<fields.length;i++){
                try {
                    Field f = obj.getClass().getDeclaredField(fields[i].getName());
                    f.setAccessible(true);
                    Object o = f.get(obj);
                    if(o != null && !o.equals("") && !(o instanceof List)){
                        reMap.put(fields[i].getName(), o);
                    }
                    if(o instanceof List){
                        List<?> list= (List<?>) o;
                        if(list.size()==0)continue;
                        Field[] fields2=list.get(0).getClass().getDeclaredFields();
                        StringBuffer stringBuffer=new StringBuffer();
                        for(int j=0;j<list.size();j++){
                            for(int m=0;m<fields2.length;m++){
                                if(isBaseType(list.get(j).getClass())){
                                    stringBuffer.append(fields[i].getName());
                                    stringBuffer.append("[");
                                    stringBuffer.append(j);
                                    stringBuffer.append("]");
                                    reMap.put(stringBuffer.toString(), list.get(j));
                                    stringBuffer.setLength(0);
                                    continue;
                                }
                                Field f2 = list.get(j).getClass().getDeclaredField(fields2[m].getName());
                                f2.setAccessible(true);
                                Object o2 = f2.get(list.get(j));
                                if(o2 != null && !o2.equals("") && !(o2 instanceof List)){
                                    stringBuffer.append(fields[i].getName());
                                    stringBuffer.append("[");
                                    stringBuffer.append(j);
                                    stringBuffer.append("].");
                                    stringBuffer.append(fields2[m].getName());
                                    reMap.put(stringBuffer.toString(),o2);
                                }
                                stringBuffer.setLength(0);
                            }
                        }
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return reMap;
    }

    /**
     * Created with ${PRODUCT_NAME}.
     * User: pual
     * Date: ${DATE}
     * Desc:
     */
    public static boolean isBaseType(Class<? extends Object> aClass) {
        if(aClass.equals(String.class)){
            return true;
        }else if(aClass.equals(Integer.class)){
            return true;
        }else if(aClass.equals(Character.class)){
            return true;
        }else if(aClass.equals(Boolean.class)){
            return true;
        }
        return false;
    }

    public static Map<String,Object> convertObjToMap(List list){
        Map<String,Object> reMap = new HashMap<String,Object>();
        if (list == null || list.size()==0){
            return null;
        }

        for(int k=0;k<list.size();k++) {
            Object obj=list.get(k);
            Field[] fields =obj.getClass().getDeclaredFields();
            try {
                for (int i = 0; i < fields.length; i++) {
                    try {
                        Field f = obj.getClass().getDeclaredField(fields[i].getName());
                        f.setAccessible(true);
                        Object o = f.get(obj);
                        if (o != null && !o.equals("") && !(o instanceof List)) {
                            reMap.put("list["+k+"]."+fields[i].getName(), o);
                        }
                        if (o instanceof List) {
                            List<?> tempList = (List<?>) o;
                            if (tempList.size() == 0) continue;
                            Field[] fields2 = tempList.get(0).getClass().getDeclaredFields();
                            StringBuffer stringBuffer = new StringBuffer();
                            for (int j = 0; j < tempList.size(); j++) {
                                for (int m = 0; m < fields2.length; m++) {
                                    Field f2 = tempList.get(j).getClass().getDeclaredField(fields2[m].getName());
                                    f2.setAccessible(true);
                                    Object o2 = f2.get(tempList.get(j));
                                    if (o2 != null && !o2.equals("") && !(o2 instanceof List)) {
                                        stringBuffer.append(fields[i].getName());
                                        stringBuffer.append("[");
                                        stringBuffer.append(j);
                                        stringBuffer.append("].");
                                        stringBuffer.append(fields2[m].getName());
                                        reMap.put(stringBuffer.toString(), o2);
                                    }
                                    stringBuffer.setLength(0);
                                }
                            }
                        }
                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SecurityException e) {
                e.printStackTrace();
            }
        }
        return reMap;

    }
}
